<!--
 * @Description: 出场检验
 * @Author: wangwangwang
 * @Date: 2020-09-21 14:09:06
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-29 11:28:12
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display:flex">
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <el-form ref="form" inline :model="tableParam" label-width="80px">
                  <el-form-item label="检测时间">
                    <el-date-picker v-model="tableParam.time" type="daterange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="工程名称">
                    <el-input v-model="tableParam.engineeringName" style="width: 180px;" placeholder="请输入" @keyup.enter.native="searchSubmit()"></el-input>
                  </el-form-item>
                  <el-form-item label="状态">
                    <el-select v-model="tableParam.detectionStatus" clearable style="width: 100px;" placeholder="请选择">
                      <el-option label="已检测" :value='1'></el-option>
                      <el-option label="待取样" :value="0"></el-option>
                      <el-option label="待检测" :value="2"></el-option>
                      <el-option label="审批中" :value="3"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item>
                    <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="searchSubmit()">查 询</el-button>
                  </el-form-item>
                  <el-button size="medium" class="bottom_all_two" style="margin-left:12px;" @click="init()">重 置</el-button>
                  <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="tableParamType = true">高级搜索</el-button>
                </el-form>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark">
                <el-table-column type="index" label="序号" width="50">
                </el-table-column>
                <el-table-column prop="station" label="站点" width="150">
                </el-table-column>
                <el-table-column prop="detectionStatus" label="状态">
                  <template slot-scope="scope">
                    <el-tag v-if="scope.row.detectionStatus === 1" type="success">已检验</el-tag>
                    <el-tag v-else-if="scope.row.detectionStatus === 0" type="success">待取样</el-tag>
                    <el-tag v-else-if="scope.row.detectionStatus === 3" type="success">审批中</el-tag>
                    <el-tag v-else type="warning">待检验</el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="detectionResult" label="自动检验结果" width="130">
                </el-table-column>
                <el-table-column label="视频">
                  <template slot-scope="scope">
                    <span class="table_link" @click="handleVideo(scope.row.videoUrl)">查看视频</span>
                  </template>
                </el-table-column>
                <el-table-column prop="engineeringName" width="200" label="工程名称">
                </el-table-column>
                <el-table-column prop="planId" label="计划单号" width="200">
                </el-table-column>
                <el-table-column prop="pouringPart" label="浇筑部位">
                </el-table-column>
                <el-table-column prop="product" label="产品名" width="300">
                </el-table-column>
                <el-table-column prop="logisticsNo" width="300" label="物流单号">
                </el-table-column>
                <el-table-column prop="carNo" width="120" label="交货车辆">
                </el-table-column>
                <el-table-column prop="detectionTime" width="120" label="提交时间">
                </el-table-column>
                <el-table-column prop="detection" label="检验人">
                </el-table-column>
                <el-table-column prop="detectionTime" label="检验时间">
                </el-table-column>
                <el-table-column fixed="right" width="140" label="操作" align="left">
                  <template slot-scope="scope">
                    <el-button v-if="scope.row.detectionStatus === 0&&ButtonList.findIndex(res=>(res.buttonValue=='QUYANG'))!==-1" class="bottom_one" size="mini" @click="makeupdStatusFun(scope.row);">
                      取样
                    </el-button>
                    <el-button v-else-if="scope.row.detectionStatus === 2&&ButtonList.findIndex(res=>(res.buttonValue=='JIANCELR'))!==-1" class="bottom_one" size="mini" @click="checkOutIn(scope.row);">
                      检测录入
                    </el-button>
                    <el-button v-else class="bottom_one" size="mini" @click="handleDetails(scope.row);">
                      查看
                    </el-button>
                    <!-- <span v-if="scope.row.detectionStatus === 0&&ButtonList.findIndex(res=>(res.buttonValue=='QUYANG'))!==-1" class="table_link" @click="makeupdStatusFun(scope.row);">取样</span> -->
                    <!-- <span v-else-if="scope.row.detectionStatus === 2&&ButtonList.findIndex(res=>(res.buttonValue=='JIANCELR'))!==-1" class="table_link" @click="checkOutIn(scope.row);">检测录入</span> -->
                    <!-- <span v-else class="table_link" @click="handleDetails(scope.row)">查看</span> -->
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div class="card_bottom">
              <div class="card_bottom_left">
                <div class="top_page_totle">总共{{ total }}条</div>
                <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
              </div>
              <div>
                <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <!-- 视频查看 -->
    <el-dialog v-dialogDrag :visible.sync="Dialog1" width="980px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">图片信息</span>
      <div class="dialoginfo">
        <div>
          <el-image v-for="(item, index) in arr1" :key="index" style="width: 200px; height:200px;margin:0 10px 10px 10px;border-radius: 4px;" :src="item" :preview-src-list="arr1" :z-index="10000"></el-image>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="Dialog1 = false">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 检测录入及查看 -->
    <el-dialog v-dialogDrag :visible.sync="visible" width="880px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">{{`检测${readOnly?'查看':'录入'}`}}</span>
      <div>
        <div class="dialoginfo-title">
          <div>
            <span>站点：</span>
            <span>{{checkOutData.station}}</span>
          </div>
          <div>
            <span>工程名称：</span>
            <span>{{checkOutData.engineeringName}}</span>
          </div>
          <div>
            <span>产品名称：</span>
            <span>{{checkOutData.product}}</span>
          </div>
          <div>
            <span>车牌号：</span>
            <span>{{checkOutData.carNo}}</span>
          </div>
        </div>
        <el-form label-width="80px">
          <el-form-item label="构件尺寸">
            <el-select v-model="checkOutData.addedValue" :disabled="readOnly" placeholder="请选择">
              <el-option label="100mm*100mm*100mm" value="100mm*100mm*100mm"></el-option>
              <el-option label="150mm*150mm*150mm" value="150mm*150mm*150mm"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="dataSource" tooltip-effect="dark" height="100%">
          <template v-for="(item, index) in tableNav">
            <el-table-column :key="index" :label="item.NAME || item.name" align="center" :width="index == 0 ? '120px' : 'auto'">
              <template slot-scope="scope">
                <span v-if="index == 0">{{ scope.row[index].name || scope.row[index].testItem }}</span>
                <div v-if="index !== 0 && scope.row[index]">
                  <el-select v-if="(scope.row[index].dataType||scope.row[index].type) == 1" v-model="scope.row[index].testValue" placeholder="请选择" style="width:100%" :disabled="readOnly">
                    <el-option v-for="(item, index) in scope.row[index].list" :key="index" :label="item.name" :value="item.name"></el-option>
                  </el-select>
                  <el-input v-if="(scope.row[index].dataType||scope.row[index].type) == 2" v-model="scope.row[index].testValue" :disabled="readOnly" style="width:100%" placeholder="请输入" clearable></el-input>
                  <span v-if="(scope.row[index].dataType||scope.row[index].type) == 3">
                    <span>{{ scope.row[index].symbol }}</span>
                    <span v-if="scope.row[index].symbol.length==1">
                      <el-input v-model="scope.row[index].testValue" :disabled="readOnly" onmousewheel="return false;" type="Number" style="width: 90px;margin-left: 5px;" placeholder="请输入" clearable></el-input>
                    </span>
                    <span v-else>
                      <el-input v-model="scope.row[index].testValue" :disabled="readOnly" onmousewheel="return false;" style="width: 90px;margin-left: 5px;" placeholder="格式：0,2" clearable></el-input>
                    </span>
                  </span>
                </div>
              </template>
            </el-table-column>
          </template>
        </el-table>
        <el-row type="flex" style="margin-top: 30px;">
          <el-col style="width:50px;">备注：</el-col>
          <el-col>
            <el-input v-model="remark" :disabled="readOnly" type="textarea" :rows="2" placeholder="请输入内容">
            </el-input>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="visible=false">取 消</el-button>
        <el-button v-if="!readOnly" class="bottom_all" @click="checkInSubmit">提交录入</el-button>
      </span>
    </el-dialog>
    <el-dialog v-dialogDrag :close-on-click-modal="false" title="结果判断" :visible.sync="dialogVisible" width="40%">
      <div v-if="result">
        系统检测结果为： OK
      </div>
      <div v-else>
        系统检测结果为： NG
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="dialogVisible=false">取 消</el-button>
        <el-button v-show="result" class="bottom_all" type="primary" @click="handleSure">确 定</el-button>
        <el-button v-show="!result" class="bottom_all" type="primary" @click="handleToFlow">进入审核</el-button>
      </span>
    </el-dialog>
    <!-- 高级搜索 -->
    <el-dialog v-dialogDrag :visible.sync="tableParamType" width="650px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">高级搜索</span>
      <div class="dialoginfo">
        <el-form ref="reftableParam" :inline="true" :model="tableParam" label-width="110px">
          <el-form-item>
            <span slot="label">物流单号</span>
            <el-input v-model="tableParam.logisticsNo" tplaceholder="请输入" clearable style="width:400px;"></el-input>
          </el-form-item>
          <el-form-item>
            <span slot="label">交货车辆</span>
            <el-input v-model="tableParam.carNo" tplaceholder="请输入" clearable style="width:400px;"></el-input>
          </el-form-item>
          <el-form-item>
            <span slot="label">检验人</span>
            <el-input v-model="tableParam.inspector" tplaceholder="请输入" clearable style="width:400px;"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="tableParamType=false,tableParam.logisticsNo='',tableParam.carNo='',tableParam.inspector=''">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="gettableData(),tableParamType=false">搜 索</el-button>
      </span>
    </el-dialog>
    <!-- 查看图片 -->
    <ShowDetails :dialog="imgsDialog" :imgList="imgList" :mp4List="mp4List" @reset="imgsDialog=false"></ShowDetails>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  concreteQueryPage,
  Download,
} from "@/api/smart/checkout.js";
import { getStationNormByDetection, getCheckResult, saveCheckOutResult, makeupdStatus2, countCheckResult } from '@/api/smart/checkout';
import ShowDetails from '@/components/showdetails';
export default {
  mixins: [mixin],
  components: {
    ShowDetails
  },
  data() {
    return {
      tableParamType: false,
      queryPage: concreteQueryPage,
      imgsDialog: false,
      imgList: [],
      mp4List: [],
      DownloadURL: Download,
      Dialog1: false,
      pagesType: 0,
      arr1: [],
      checkOutData: { addedValue: '' },
      readOnly: false,
      visible: false,
      tableNav: [],
      dataSource: [],
      remark: '',
      checkOutType: [],
      checkType: '',
      checkId: '',
      consignmentId: '',
      dialogVisible: false,
      result: '',
    };
  },
  created() {
    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = { ...this.tableParam, detectionTypeId: 5, logisticsNo: '', carNo: '', inspector: '' };
    this.gettableData();
  },
  methods: {
    init() {
      this.tableParam.engineeringName = ''
      this.tableParam.time = ''
      this.tableParam.detectionStatus = ''
      this.searchSubmit()
    },
    makeupdStatusFun(d) {
      this.$confirm('是否确认取样?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      }).then(() => {
        let json = {
          id: d.id,
          detectionStatus: 2
        }
        makeupdStatus2(json).then((res) => {
          if (res.code == 1001) {
            this.searchSubmit()
          }
        })
      }).catch(() => {
      });
    },
    searchSubmit() {
      if (this.tableParam ?.time ?.length) {
        this.tableParam.createTime = this.tableParam.time[0]
        this.tableParam.updateTime = this.tableParam.time[1]
      } else {
        this.tableParam.createTime = ''
        this.tableParam.updateTime = ''
      }
      this.gettableData();
    },
    handleVideo(url) {
      if (!url) {
        this.$message.warning('视频为空')
        return
      }
      let items = url.split(",").map(res => ('/file/' + res))
      this.imgList = items.filter(res => (res.includes('type=img')))
      this.mp4List = items.filter(res => (res.includes('type=mp4')))
      this.imgsDialog = true
    },
    checkOutIn(data) {
      this.remark = '';
      this.checkId = data.id;
      this.consignmentId = data.consignmentId;
      const params = {
        id: data.detectionTypeId,
        dataSources: 2,
        type: 2,
        strengthGradeId: data.productId,
        stationId: data.stationId,
      }
      getStationNormByDetection(params).then(res => {
        if (res.code === 1001) {
          this.checkOutData = data;
          this.visible = true;
          this.readOnly = false;
          /**
           * @Description: 构造数据
           * @Param: 
           * @Author: wangwangwang
           */
          this.tableNav = res.data.claim; // 表头数据
          this.dataSource = res.data.list; // 表单数据
          this.tableNav.unshift({
            NAME: "验收项目"
          })
        }
      })
    },
    /**
     * @Description: 查看数据详情
     * @Param: 
     * @Author: wangwangwang
     */
    handleDetails(data) {
      const params = {
        pid: data.id,
      }
      this.remark = data.handleDetails;
      getCheckResult(params).then(res => {
        if (res.code === 1001) {
          this.checkOutData = data;
          this.visible = true;
          this.readOnly = true;
          this.tableNav = res.data.title; // 表头数据
          const newData = res.data.list.map(item => {
            item.unshift({
              testItem: item[0].testItem
            })
            return item
          });
          this.dataSource = newData; // 表单数据
          this.tableNav.unshift({
            NAME: "验收项目"
          })
        }
      })
    },
    checkInSubmit() {
      if (!this.checkOutData.addedValue) {
        this.$message({
          message: '请选择构件尺寸',
          type: 'warning'
        });
        return
      }
      const copyData = JSON.parse(JSON.stringify(this.tableNav));
      copyData.shift();
      const params = {
        id: this.checkOutData.id,
        typeId: this.checkOutData.detectionTypeId,
        claim: copyData,
        list: this.dataSource,
        type: 2,
      }
      /**
       * @Description: 录入检测结果、如果结果为NG 则流程
       * @Param: 
       * @Author: wangwangwang
       */
      countCheckResult(params).then(res => {
        if (res.code === 1001) {
          this.visible = false;
          this.result = res.data;
          this.dialogVisible = true;
        }
      })
    },
    /**
     * @Description: 结果为OK点击确定改变状态
     * @Param: 
     * @Author: wangwangwang
     */
    handleSure() {
      const copyData = JSON.parse(JSON.stringify(this.tableNav));
      copyData.shift();
      const params = {
        id: this.checkOutData.id,
        typeId: this.checkOutData.detectionTypeId,
        claim: copyData,
        remark: this.remark,
        outcome: 'OK',
        list: this.dataSource,
        type: 2
      }
      /**
       * @Description: 录入检测结果、如果结果为NG 则流程
       * @Param: 
       * @Author: wangwangwang
       */
      saveCheckOutResult(params).then(res => {
        if (res.code === 1001) {
          this.visible = false;
          this.result = res.data;
          this.dialogVisible = false;
          this.gettableData();
        }
      })
    },
    /**
     * @Description: 结果为NG进入审核
     * @Param: 
     * @Author: wangwangwang
     */
    handleToFlow() {
      const params = {
        station: this.checkOutData.station,
        engineeringName: this.checkOutData.engineeringName,
        product: this.checkOutData.product,
        detectionTypeId: this.checkOutData.detectionTypeId,
        stationId: this.checkOutData.stationId,
        productId: this.checkOutData.productId,
        carNo: this.checkOutData.carNo,
        contractId: this.checkOutData.contractId,
        id: this.checkOutData.id,
        type: 1,
        flowsType: 1, // 出场检测
        tableTitle: JSON.stringify(this.tableNav),
        tableDataSource: JSON.stringify(this.dataSource),
      }
      this.$router.push({ name: "checkflows2", query: params });
    }
  },
};
</script>

<style lang="scss" scoped>
.dialoginfo-title {
  display: flex;
  justify-content: space-between;
  padding: 10px 0 20px 0;
  font-weight: bold;
}
.el-input,
.el-select {
  width: 240px;
}
.table_link {
  margin: 0 10px;
  color: $bg_color;
}

.table_link:hover {
  opacity: 0.8;
}
</style>
